from pydantic import BaseModel
import os


class Settings(BaseModel):
    qwen_provider: str = os.getenv("QWEN_PROVIDER", "local")
    qwen_edge_path: str = os.getenv("QWEN_EDGE_PATH", "")
    qwen_http_endpoint: str = os.getenv("QWEN_HTTP_ENDPOINT", "")
    qwen_hybridflow_endpoint: str = os.getenv("QWEN_HYBRIDFLOW_ENDPOINT", "")
    mysql_uri: str = os.getenv("MYSQL_URI", "")
    redis_uri: str = os.getenv("REDIS_URI", "redis://localhost:6379/0")
    milvus_uri: str = os.getenv("MILVUS_URI", "")
    neo4j_uri: str = os.getenv("NEO4J_URI", "")
    elastic_endpoint: str = os.getenv("ELASTIC_ENDPOINT", "")
    fleet_server: str = os.getenv("FLEET_SERVER", "")
    otel_endpoint: str = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "")
    java_signature_secret: str = os.getenv("JAVA_SIGNATURE_SECRET", "")
    qwen_sft_data_dir: str = os.getenv("QWEN_SFT_DATA_DIR", "datasets/qwen_sft")
    qwen_output_dir: str = os.getenv("QWEN_OUTPUT_DIR", "artifacts/qwen")
    asr_finetune_data_dir: str = os.getenv("ASR_FINETUNE_DATA_DIR", "datasets/asr")
    tts_finetune_data_dir: str = os.getenv("TTS_FINETUNE_DATA_DIR", "datasets/tts")
    asr_output_dir: str = os.getenv("ASR_OUTPUT_DIR", "artifacts/asr")
    tts_output_dir: str = os.getenv("TTS_OUTPUT_DIR", "artifacts/tts")
    qwen_provider_priority: str = os.getenv("QWEN_PROVIDER_PRIORITY", "local,http,hybridflow,vllm")
    qwen_vllm_endpoint: str = os.getenv("QWEN_VLLM_ENDPOINT", "")
    asr_lang: str = os.getenv("ASR_LANG", "zh")
    tts_voice: str = os.getenv("TTS_VOICE", "zh_male")
    ocr_lang: str = os.getenv("OCR_LANG", "ch")
    http_client_timeout: float = float(os.getenv("HTTP_CLIENT_TIMEOUT", "5.0"))
    http_client_max_connections: int = int(os.getenv("HTTP_CLIENT_MAX_CONNECTIONS", "100"))
    http_retry_attempts: int = int(os.getenv("HTTP_RETRY_ATTEMPTS", "3"))
    http_retry_backoff_max: float = float(os.getenv("HTTP_RETRY_BACKOFF_MAX", "2.0"))
    provider_error_streak_max: int = int(os.getenv("PROVIDER_ERROR_STREAK_MAX", "3"))
    provider_cooldown_seconds: float = float(os.getenv("PROVIDER_COOLDOWN_SECONDS", "30"))
    mysql_pool_min_size: int = int(os.getenv("MYSQL_POOL_MIN_SIZE", "1"))
    mysql_pool_max_size: int = int(os.getenv("MYSQL_POOL_MAX_SIZE", "10"))
    mysql_connect_timeout: int = int(os.getenv("MYSQL_CONNECT_TIMEOUT", "3"))
    mysql_read_uri: str = os.getenv("MYSQL_READ_URI", "")
    mysql_slow_threshold_ms: int = int(os.getenv("MYSQL_SLOW_THRESHOLD_MS", "500"))
    mysql_max_exec_time_ms: int = int(os.getenv("MYSQL_MAX_EXEC_TIME_MS", "0"))
    db_error_rate_alert_threshold: float = float(os.getenv("DB_ERROR_RATE_ALERT_THRESHOLD", "0.05"))
    db_slow_query_alert_threshold: int = int(os.getenv("DB_SLOW_QUERY_ALERT_THRESHOLD", "100"))
    provider_p95_max_ms: int = int(os.getenv("PROVIDER_P95_MAX_MS", "1000"))
    provider_queue_max: float = float(os.getenv("PROVIDER_QUEUE_MAX", "1"))
    provider_gpu_mem_max_mb: float = float(os.getenv("PROVIDER_GPU_MEM_MAX_MB", "16000"))
    provider_kv_min_hit_rate: float = float(os.getenv("PROVIDER_KV_MIN_HIT_RATE", "0.1"))
    enable_llm_intent: bool = os.getenv("ENABLE_LLM_INTENT", "true").lower() in {"1", "true", "yes"}
    intent_llm_weight: float = float(os.getenv("INTENT_LLM_WEIGHT", "0.5"))
    intent_low_conf_threshold: float = float(os.getenv("INTENT_LOW_CONF_THRESHOLD", "0.5"))
    pay_amount_confirm_threshold: float = float(os.getenv("PAY_AMOUNT_CONFIRM_THRESHOLD", "10000"))
    refund_amount_confirm_threshold: float = float(os.getenv("REFUND_AMOUNT_CONFIRM_THRESHOLD", "5000"))
    rate_limits_per_min: int = int(os.getenv("RATE_LIMITS_PER_MIN", "5"))
    rate_limits_per_10min: int = int(os.getenv("RATE_LIMITS_PER_10MIN", "20"))
    rate_limits_per_hour: int = int(os.getenv("RATE_LIMITS_PER_HOUR", "60"))
    risk_score_confirm_threshold: float = float(os.getenv("RISK_SCORE_CONFIRM_THRESHOLD", "0.7"))
    account_blacklist_refresh_s: int = int(os.getenv("ACCOUNT_BLACKLIST_REFRESH_S", "300"))
    # External systems unified config
    payments_base_url: str = os.getenv("PAYMENTS_BASE_URL", "")
    checkin_base_url: str = os.getenv("CHECKIN_BASE_URL", "")
    rebook_base_url: str = os.getenv("REBOOK_BASE_URL", "")
    external_auth_token: str = os.getenv("EXTERNAL_AUTH_TOKEN", "")
    external_timeout_s: float = float(os.getenv("EXTERNAL_TIMEOUT_S", "2.0"))
    # Experiments online assignment
    experiments_assign_enabled: bool = os.getenv("EXPERIMENTS_ASSIGN_ENABLED", "false").lower() in {"1", "true", "yes"}
    experiments_assign_timeout_ms: int = int(os.getenv("EXPERIMENTS_ASSIGN_TIMEOUT_MS", "200"))
    experiments_assign_candidates: str = os.getenv("EXPERIMENTS_ASSIGN_CANDIDATES", "")
    # FIDS/Flight data integration
    fids_base_url: str = os.getenv("FIDS_BASE_URL", "")
    fids_auth_token: str = os.getenv("FIDS_AUTH_TOKEN", "")
    fids_timeout_s: float = float(os.getenv("FIDS_TIMEOUT_S", "2.0"))
    # Scheduler weights
    scheduler_w_dist: float = float(os.getenv("SCHEDULER_W_DIST", "1.0"))
    scheduler_w_time: float = float(os.getenv("SCHEDULER_W_TIME", "1.0"))
    scheduler_w_batt: float = float(os.getenv("SCHEDULER_W_BATT", "1.0"))
    scheduler_w_crowd: float = float(os.getenv("SCHEDULER_W_CROWD", "0.5"))
    scheduler_w_skill: float = float(os.getenv("SCHEDULER_W_SKILL", "1.0"))
    # Webhook/WebSocket
    webhook_timeout_s: float = float(os.getenv("WEBHOOK_TIMEOUT_S", "2.0"))
    websocket_idle_timeout_s: float = float(os.getenv("WEBSOCKET_IDLE_TIMEOUT_S", "300"))
    robot_execution_mode: str = os.getenv("ROBOT_EXECUTION_MODE", "stub")
    auth_enabled: bool = os.getenv("AUTH_ENABLED", "false").lower() in {"1", "true", "yes"}
    jwt_secret: str = os.getenv("JWT_SECRET", "")
    jwt_alg: str = os.getenv("JWT_ALG", "HS256")
    robot_namespace_prefix: str = os.getenv("ROBOT_NAMESPACE_PREFIX", "robot")
    robot_nav_frame: str = os.getenv("ROBOT_NAV_FRAME", "map")
    robot_namespace_map: str = os.getenv("ROBOT_NAMESPACE_MAP", "")
    robot_nav_auto_retry: bool = os.getenv("ROBOT_NAV_AUTO_RETRY", "true").lower() in {"1", "true", "yes"}
    robot_nav_retry_max: int = int(os.getenv("ROBOT_NAV_RETRY_MAX", "1"))
    robot_nav_retry_backoff_ms: int = int(os.getenv("ROBOT_NAV_RETRY_BACKOFF_MS", "0"))


def load_settings() -> Settings:
    return Settings()